// Copyright 2012. InMobi. All Rights reserved

package com.inmobi.sharedlock;

/**
 * Average calculator which works by synchronizing the object.
 */
public class AlmostThreadSafeCalculator implements AverageCalculator {

  private long sum = 0;

  private int count = 0;

  @Override
  public synchronized void addValue(long value) {
    count++;
    sum += value;
  }

  @Override
  public synchronized double getAverage() {
    return count != 0 ? ((double) sum) / count : 0.0;
  }
}
